package lab1;
/*  $Id: L2.java
 *  
 *  Copyright 2012, The Johns Hopkins University Whiting School of Engineering
 *  All rights reserved.
 *  This material may be used, modified and reproduced by faculty,
 *  staff, and students of The Johns Hopkins University for instruction, 
 *  evaluation, and grading purposes.  For any other permission, please 
 *  contact The Johns Hopkins University Whiting School of Engineering.
 */
import lab1.mStack.EmptyStackException;
/**
*  Lab1: L2 - Language 2 
*  @date       30JUNE2012
*  @author     Michael W. Mathes
*/
public class L2 {
  public boolean L2(String s){
    mStack st = new mStack(s.length());    
    mStack err = new mStack(s.length());
    boolean success = false;
    
    for (int i = 0; i < s.length(); i++) {
      char C = s.charAt(i);
           
      //error checking: if it equals something other then A+B push the error stack
      if ( C != 'A' )          // Check A
        if (C != 'B') {        // Check B
          err.push(C);
        }
          
      // if Char = A push it onto the stack
      if ( C == 'A'){
        // Interleaved ABAB should fail:
        // checking for an empty stack after the first run of the loop
          
        if (i>0){
          if (st.isEmpty()){
            err.push(C);
          }      
        }
          
        st.push(C);
      }
      
      // if char c == B, pop the b's off the stack
      if (C == 'B'){
          
        // if we have more b's then a's, we have a problem.
        // set the error flag and fail  
        if (st.isEmpty()){
            err.push(C);
        }    
        
        // pop the b's off the stack
        try {    
          st.pop();
        } catch (EmptyStackException e) {}
      }
    }//endfor

    // Return Block   
    if (st.isEmpty()){
      success = true;
    } if (!err.isEmpty()) {
    success =  false;
    }  
      
    return success;
    }
}